package itsur.arquitecturas.implicitinvocation;

/**************************************************************************************
** Class name: LineOfTextFileReader
** Author: A.J. Lattanze
** Date: 12/3/99
** Version 1.0
***************************************************************************************
** Purpose: Assignment 3 for SE 17-655, Architectures for Software Systems.
** (reused from assignment 2)    
**
** Abstract: This class provides the methods that allow the caller to open an existing
** file and read one line of input (to end-of-line) from the file.
** 
** Modification Log
**************************************************************************************
**
**************************************************************************************/
import java.io.*;

public class LineOfTextFileReader
{

   File InputFile = null;
   BufferedReader InFile = null;

   public boolean OpenFile( String PathName )
   {
	boolean Result;
	InputFile = new File( PathName );

	// Open input file.  The input file is a field oriented and space-separated.  
	// The fields and expected formats are listed above in the header

	// Check to ensure that the InputFile exists

	if ( !InputFile.exists() )
	{
	   Result = false;
	         
	   // Debug Statements
	   //
	   System.out.print( "LineOfTextFileReader::File " + InputFile.getAbsolutePath() );
         System.out.println( " NOT FOUND." );

      } else {

 	   // Debug Statements
	   //
	   //System.out.print( "LineOfTextFileReader::file" + InputFile.getAbsolutePath() );
         //System.out.println( " opened." );

         try
         {
	   	// Create a buffered reader the file
            		
		InFile = new BufferedReader(new InputStreamReader(
            new FileInputStream((InputFile))));

 	      Result = true;

	   } // try

	   catch( Exception Error )
         {
         	// Debug Statement
		//
		// System.out.println( "LineOfTextLineOfText::Unable to create stream file" );

		Result = false;

         } // catch

	} // if

	return ( Result );

   } // OpenFile


   public String ReadLineOfText()
   {
	String LineOfText = null;

      // Read a line of text from the input file and convert it to upper case 

      try
      {
	   LineOfText = InFile.readLine();

	} // try

	catch( Exception Error )
      {
         // Debug Statement
	   //
	   // System.out.println( "LineOfTextFileReader::Read Error" );
     	   
	   try
	   {
	   	throw( Error );

	   } // try
	   catch( Exception e )
	   { 
		// We are in real trouble if we get here.
         	// Debug Statement
	   	//
	   	// System.out.println( "LineOfTextFileReader::Re-throw Error" );
	   }  // catch
      } // catch

	return( LineOfText );

   } // ReadLineOfText

   public boolean CloseFile()
   {
	boolean Result = true;

	// Close the input file

      try
      {
	   InFile.close();
        
	   // Debug Statements
	   //
	   // System.out.print( "\nLineOfTextLineOfText::closing " );
         // System.out.println( InputFile.getAbsolutePath() );

	} // try

	catch( Exception Error )
      {
         // Debug Statement
	   //
	   // System.out.println( "LineOfTextFileReader::Unable to close stream file" );

	   Result = false;	   

      } // catch

	return( Result );

   } // ReadLineOfText

} // LineOfTextFileReader